<?php
/**
 * Author: 式神(vashi)
 * email: 289650682@qq.com
 * Date: 2019/2/20 10:07
 * FileName: Category.php
 */

namespace app\cms\controller;


use app\common\controller\Common;

class Category extends Common
{
    protected $middleware = ['Auth'];
    public function initialize()
    {
        parent::initialize();
        $this->db = new \app\cms\model\Category();

    }

    public function index(){
        $input=input('get.');

        $map = $this->getMap($input);
        $order = $this->getOrder($input);

        $res['total'] = $this->db->where($map)->order($order)->count();
        $data= $this->db->where($map)->order($order)->select();

        $res['rows']=data_tree($data);

        $d=[
            "code"=>0,
        ];
        $d['data']=$res;
        return json($d);
    }

    //添加
    public function add(){
        $res=$this->db->add(input('post.'));
        return json($res);
    }
    //编辑
    public function edit(){
        $res=$this->db->edit(input('post.'));
        return json($res);
    }
    //删除
    public function del(){
        $input=input('post.');
        $childId = $this->db->where('pid',$input[0])->field('id')->select();
        if(count($childId)>0){
            $res=$this->res;
            $res['msg']="子栏目不为空，不能删除";
        }else{
            $res=$this->db->del(input('post.'));
        }
        return json($res);
    }

    //规则数
    public function catetree(){
        $get = input('get.');
        $cate = $this->db->select();
        $arrid=[];
        if($get['id']!=0){
            $arrid=getChildsId($cate,$get['id']);
            array_push($arrid,$get['id']);
        }
        $data=data_tree($cate,'pid',$get['pid'],$arrid);
        array_unshift($data,['key'=>0,'title'=>'作为顶级栏目','value'=>0]);
        $d=[
            "code"=>0,
        ];
        $d['data']=$data;
        return json($d);
    }
}